﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text

Public Class clsUsuarios
    Inherits clsDatos

    Private des As New TripleDESCryptoServiceProvider 'Algorithmo TripleDES
    Private hashmd5 As New MD5CryptoServiceProvider 'objeto md5
    Private myKey As String = "p" 'Clave secreta(puede alterarse)

    Private m_strNombreCompleto As String
    Public Property NombreCompleto() As String
        Get
            Return m_strNombreCompleto
        End Get
        Set(ByVal value As String)
            m_strNombreCompleto = value
        End Set
    End Property


    Private m_str_eMail As String
    Public Property eMail() As String
        Get
            Return m_str_eMail
        End Get
        Set(ByVal value As String)
            m_str_eMail = value
        End Set
    End Property


    Private m_strContrasena As String
    Public Property Contrasena() As String
        Get
            Return m_strContrasena
        End Get
        Set(ByVal value As String)
            m_strContrasena = value
        End Set
    End Property

    Public Function DoAutenticarUsuario(ByVal strUserName As String, ByVal strPass As String) As Boolean
        Dim objUsuario As SqlDataReader

        objUsuario = GetRegistro("Select * from Usuario where usuario='" & strUserName & "'")
        If objUsuario.HasRows Then
            Do While objUsuario.Read
                If MD5EncryptPass(strPass.ToUpper) <> objUsuario("contrasena").ToString Then
                    Return False
                    Exit Function

                End If

            Loop

        End If

        Return True
        Exit Function

    End Function

    Public Function DoBuscaUsuario(ByVal strUserName As String) As Boolean
        Dim objUsuario As SqlDataReader

        objUsuario = GetRegistro("Select * from Usuario where usuario='" & strUserName & "'")
        If objUsuario.HasRows Then
            Do While objUsuario.Read
                Me.NombreCompleto = objUsuario("nombre").ToString
                Me.Contrasena = objUsuario("contrasena").ToString
                Me.eMail = objUsuario("eMail").ToString

                Return True
                Exit Function
            Loop

        End If

        Me.NombreCompleto = ""
        Me.Contrasena = ""
        Return False

    End Function

    Public Function GetConfigArt(ByVal strUserName As String) As Boolean
        Dim objUsuario As SqlDataReader

        objUsuario = GetRegistro("select * from usuarioacceso where usuario='" & strUserName & "' and menuprincipal like '%Cta.Art%'")
        If objUsuario.HasRows Then
            Do While objUsuario.Read
                Return True
                Exit Function

            Loop

        End If

        Return False

    End Function

    'Funcion para el Desencriptado de Cadenas de Texto
    Public Function DoDesencriptar(ByVal texto As String) As String
        If Trim(texto) = "" Then
            DoDesencriptar = ""
        Else
            des.Key = hashmd5.ComputeHash((New UnicodeEncoding).GetBytes(myKey))
            des.Mode = CipherMode.ECB
            Dim desencrypta As ICryptoTransform = des.CreateDecryptor()
            Dim buff() As Byte = Convert.FromBase64String(texto)
            DoDesencriptar = UnicodeEncoding.ASCII.GetString(desencrypta.TransformFinalBlock(buff, 0, buff.Length))
        End If
        Return DoDesencriptar
    End Function

    'Funcion para el Encriptado de Cadenas de Texto
    Public Function DoEncriptar(ByVal texto As String) As String

        If Trim(texto) = "" Then
            DoEncriptar = ""
        Else
            des.Key = hashmd5.ComputeHash((New UnicodeEncoding).GetBytes(myKey))
            des.Mode = CipherMode.ECB
            Dim encrypt As ICryptoTransform = des.CreateEncryptor()
            Dim buff() As Byte = UnicodeEncoding.ASCII.GetBytes(texto)
            DoEncriptar = Convert.ToBase64String(encrypt.TransformFinalBlock(buff, 0, buff.Length))
        End If
        Return DoEncriptar
    End Function

    Public Function MD5EncryptPass(ByVal StrPass As String) As String

        Dim md5 As MD5CryptoServiceProvider
        Dim bytValue() As Byte
        Dim bytHash() As Byte
        Dim strPassOutput As String
        Dim i As Integer
        strPassOutput = ""

        md5 = New MD5CryptoServiceProvider

        bytValue = System.Text.Encoding.UTF8.GetBytes(StrPass)

        bytHash = md5.ComputeHash(bytValue)
        md5.Clear()

        For i = 0 To bytHash.Length - 1
            strPassOutput &= bytHash(i).ToString("x").PadLeft(2, "0")
        Next

        Return strPassOutput

    End Function
End Class